package com.rabbit2020.config;

import com.alibaba.fastjson.JSON;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 作者: LCG
 * 日期: 2020/2/27 23:43
 * 描述: 发送确认保证消息发送的时候 确定到达交换器
 */
@Component
public class RabbitTemplateConfirm implements RabbitTemplate.ConfirmCallback {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void initRabbitTemplate() {
        // 设置生产者消息确认
        rabbitTemplate.setConfirmCallback(this);

    }

    @Override
    public void confirm(CorrelationData correlationData, boolean b, String s) {
        if(b){
            System.out.println("--发送成功确认-->"+s+"--------"+ JSON.toJSONString(correlationData));
        }else{
            System.out.println("=========发送失败=======");
        }
    }
}
